package com.jz.yanshe.ui;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.jz.jd.rest.business.RestProxy;
import com.jz.jd.rest.response.FactoryResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import com.jz.yanshe.util.ImageLoaderHelper;
import com.jz.yanshe.base.BaseActivity;
import com.jz.yanshe.dialog.BuyDialog;
import com.jz.yanshe.util.PreferencesHelper;
import jz.com.feierman.R;

/**
 * Created by Administrator on 2018/3/15.
 */

public class FactoryActivity extends BaseActivity implements OnRefreshListener, OnLoadMoreListener {

    @BindView(R.id.swipe_target)
    RecyclerView recyclerView;
    @BindView(R.id.swipeToLoadLayout)
    SwipeToLoadLayout swipeToLoadLayout;
    MyAdapter myAdapter = new MyAdapter();
    List<FactoryResponse.VIPGoods> list = new ArrayList<>();
    private BuyDialog mBuyDialog;
    int page = 1;
    int sortByType = 1;  //1：销量 2：价格 3：时间
    int descOrAsc =1; // 1：降序 2：升序
    @BindView(R.id.tv_sale_num)
    TextView tv_sale_num;

    @BindView(R.id.img_sale_num)
    ImageView img_sale_num;


    @BindView(R.id.tv_price)
    TextView tv_price;

    @BindView(R.id.img_price)
    ImageView img_price;

    @BindView(R.id.tv_new)
    TextView tv_new;

    @BindView(R.id.img_new)
    ImageView img_new;

    @Override
    protected void initViews() {

        recyclerView.setAdapter(myAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.bottom = 20;
            }
        });
        swipeToLoadLayout.setOnRefreshListener(this);
        swipeToLoadLayout.setOnLoadMoreListener(this);

    }

    @Override
    protected void initData() {
        getList(sortByType, descOrAsc, page, 20);
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_factory;
    }

    @Override
    public void onLoadMore() {
        page++;
        getList(sortByType, descOrAsc, page, 20);
    }

    @Override
    public void onRefresh() {
        page =1;
        getList(sortByType, descOrAsc, page, 20);
    }


    class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {


        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            Holder holder = new Holder(getLayoutInflater().inflate(R.layout.item_factory, parent, false));
            return holder;
        }

        @Override
        public void onBindViewHolder(final Holder holder, final int position) {
            if(list.get(position).goods_imgs !=null && !list.get(position).goods_imgs.equals("")) {
                ImageLoaderHelper.getInstance().GlideImageLoader(FactoryActivity.this, list.get(position).goods_icon, holder.img_goods);
            }holder.tv_goods_name.setText(list.get(position).goods_name);
            holder.tv_price.setText("¥ " + list.get(position).goods_price);
            holder.tv_price.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            holder.tv_price_new.setText("¥ " + list.get(position).new_price);
            holder.tv_discount.setText(list.get(position).discount);
            holder.btn_add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showBuyDialog(list.get(position));
                }
            });
            holder.btn_share.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("obj",list.get(position));
                    startActivity(QRActivity.class,bundle);

                }
            });
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putString("goodsid",list.get(position).goods_id);
                    startActivity(GoodsDetailActivity.class,bundle);
                }
            });
//

        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class Holder extends RecyclerView.ViewHolder {
            ImageView img_goods, btn_share, btn_add;
            TextView tv_goods_name, tv_price, tv_price_new, tv_discount;

            public Holder(View itemView) {
                super(itemView);
                img_goods = itemView.findViewById(R.id.img_goods);
                btn_share = itemView.findViewById(R.id.btn_share);
                tv_goods_name = itemView.findViewById(R.id.tv_goods_name);
                btn_add = itemView.findViewById(R.id.btn_add_s);
                tv_price = itemView.findViewById(R.id.tv_price);
                tv_price_new = itemView.findViewById(R.id.tv_price_new);
                tv_discount = itemView.findViewById(R.id.tv_discount);
            }
        }
    }

    private void getList(int sortByType, int descOrAsc,final int page, int pagesize) {
        RestProxy.getInstance().getFactory(PreferencesHelper.getInstance().getString(this, "token"), sortByType+"", descOrAsc+"", page+"", pagesize+"", new Observer<FactoryResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(FactoryResponse factoryResponse) {
                if(page == 1){
                    list.clear();
                }
                list.addAll(factoryResponse.listM);
                myAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {
                swipeToLoadLayout.setRefreshing(false);
                swipeToLoadLayout.setLoadingMore(false);
            }

            @Override
            public void onComplete() {
                swipeToLoadLayout.setRefreshing(false);
                swipeToLoadLayout.setLoadingMore(false);
            }
        });
    }


    private void showBuyDialog(FactoryResponse.VIPGoods mVIPGoods) {
        if (mBuyDialog == null) {
            mBuyDialog = mBuyDialog.newInstance();
            mBuyDialog.show(getSupportFragmentManager(), "mTextDialogFragment");
//            mBuyDialog.setmListener(new mBuyDialog.OnItemClickListener() {
//                @Override
//                public void onConfirm(String name) {

//                }
//            });
        } else if (!mBuyDialog.isAdded() && !mBuyDialog.isShowing()) {
            mBuyDialog.show(getSupportFragmentManager(), "mAgeDialogFragment");
        }
        mBuyDialog.getAllSize(mVIPGoods.goods_id);
        mBuyDialog.goodsid = mVIPGoods.goods_id;
        mBuyDialog.setData(mVIPGoods.new_price, mVIPGoods.goods_inventory, mVIPGoods.goods_icon);
    }

    @OnClick(R.id.btn_sale_num)
    void btn_sale_num(){
        page =1;
        if(sortByType !=1){
            descOrAsc = 1;
        }
        if(sortByType == 1){
            if(descOrAsc == 1){
                descOrAsc = 2;
                getList(sortByType,descOrAsc,page,20);
                img_sale_num.setImageResource(R.mipmap.icon_vip_more_up_red);
            }else {
                descOrAsc =1;
                getList(sortByType,descOrAsc,page,20);
                img_sale_num.setImageResource(R.mipmap.icon_vip_more_down_red);
            }
            tv_sale_num.setTextColor(getResources().getColor(R.color.commonRed));
            tv_price.setTextColor(getResources().getColor(R.color.commonBlack));
            tv_new.setTextColor(getResources().getColor(R.color.commonBlack));
            img_price.setImageResource(R.mipmap.icon_vip_more_down_grey);
            img_new.setImageResource(R.mipmap.icon_vip_more_down_grey);
        }else{
            descOrAsc = 1;
            sortByType = 1;
            getList(sortByType,descOrAsc,page,20);
            img_sale_num.setImageResource(R.mipmap.icon_vip_more_down_red);
            tv_sale_num.setTextColor(getResources().getColor(R.color.commonRed));
            tv_price.setTextColor(getResources().getColor(R.color.commonBlack));
            tv_new.setTextColor(getResources().getColor(R.color.commonBlack));
            img_price.setImageResource(R.mipmap.icon_vip_more_down_grey);
            img_new.setImageResource(R.mipmap.icon_vip_more_down_grey);
        }
    }

    @OnClick(R.id.btn_price)
    void btn_price(){
        page =1;
        if(sortByType !=2){
            descOrAsc = 1;
        }
        if(sortByType == 2){
            if(descOrAsc == 1){
                descOrAsc = 2;
                getList(sortByType,descOrAsc,page,20);
                img_price.setImageResource(R.mipmap.icon_vip_more_up_red);
            }else {
                descOrAsc =1;
                getList(sortByType,descOrAsc,page,20);
                img_price.setImageResource(R.mipmap.icon_vip_more_down_red);
            }
            tv_price.setTextColor(getResources().getColor(R.color.commonRed));
            tv_sale_num.setTextColor(getResources().getColor(R.color.commonBlack));
            tv_new.setTextColor(getResources().getColor(R.color.commonBlack));
            img_sale_num.setImageResource(R.mipmap.icon_vip_more_down_grey);
            img_new.setImageResource(R.mipmap.icon_vip_more_down_grey);
        }else{
            descOrAsc =1;
            sortByType = 2;
            getList(sortByType,descOrAsc,page,20);
            img_price.setImageResource(R.mipmap.icon_vip_more_down_red);
            tv_price.setTextColor(getResources().getColor(R.color.commonRed));
            tv_sale_num.setTextColor(getResources().getColor(R.color.commonBlack));
            tv_new.setTextColor(getResources().getColor(R.color.commonBlack));
            img_sale_num.setImageResource(R.mipmap.icon_vip_more_down_grey);
            img_new.setImageResource(R.mipmap.icon_vip_more_down_grey);
        }
    }

    @OnClick(R.id.btn_new)
    void btn_new(){
        page =1;
        if(sortByType !=3){
            descOrAsc = 1;
        }
        if(sortByType == 3){
            if(descOrAsc == 1){
                descOrAsc = 2;
                getList(sortByType,descOrAsc,page,20);
                img_new.setImageResource(R.mipmap.icon_vip_more_up_red);
            }else {
                descOrAsc =1;
                getList(sortByType,descOrAsc,page,20);
                img_new.setImageResource(R.mipmap.icon_vip_more_down_red);
            }
            tv_new.setTextColor(getResources().getColor(R.color.commonRed));
            tv_price.setTextColor(getResources().getColor(R.color.commonBlack));
            tv_sale_num.setTextColor(getResources().getColor(R.color.commonBlack));
            img_price.setImageResource(R.mipmap.icon_vip_more_down_grey);
            img_sale_num.setImageResource(R.mipmap.icon_vip_more_down_grey);
        }else{
            descOrAsc =1;
            sortByType= 3;
            getList(sortByType,descOrAsc,page,20);
            img_new.setImageResource(R.mipmap.icon_vip_more_down_red);
            tv_new.setTextColor(getResources().getColor(R.color.commonRed));
            tv_price.setTextColor(getResources().getColor(R.color.commonBlack));
            tv_sale_num.setTextColor(getResources().getColor(R.color.commonBlack));
            img_price.setImageResource(R.mipmap.icon_vip_more_down_grey);
            img_sale_num.setImageResource(R.mipmap.icon_vip_more_down_grey);
        }
    }
}
